package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.nwld.dataserver.manager.protection.OperationRiskOutManager;
import com.sduept.nwld.dataserver.model.daily.MainProtectQuit;
import com.sduept.utils.DateUtil;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.text.SimpleDateFormat;
import java.util.*;

@Named
@ViewScoped
public class OperationRiskOutController extends DailyReportAbstractController {

	private static final long serialVersionUID = 2571889314284601559L;
	
	@Autowired
	private OperationRiskOutManager ma;
	private List<MainProtectQuit> outList = new ArrayList<>();
	private MainProtectQuit created = new MainProtectQuit();
	private MainProtectQuit selected = null;
	private List<String> subareas = new ArrayList<>();

	@PostConstruct
	public void init() {
		outList = ma.findByTime(queryDate1, queryDate2);
		subareas.add("广东");
		subareas.add("广西");
		subareas.add("云南");
		subareas.add("贵州");
		subareas.add("海南");
		subareas.add("超高压");
	}

	public void refreshPage() {
		initParamter();
		init();
	}

	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		selected = null;
		created = null;
		queryByTime();
	}

	public void queryByTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start;
		String end;
		if (queryDate1 == null && queryDate2 == null) {
			outList = ma.findAll();
		} else {
			start = "1970-01-01 00:00:00";
			end = format.format(Calendar.getInstance().getTime());
			if (queryDate1 != null) {
				start = format.format(queryDate1);
			}
			if (queryDate2 != null) {
				end = format.format(queryDate2);
			}
			Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
			Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
			Calendar cend = Calendar.getInstance();
			cend.setTime(e);
			cend.add(Calendar.DAY_OF_YEAR, 1);
			outList = ma.findByTime(s, cend.getTime());
			refreshQueryParamter();
			selected = null;
		}

	}

	/**
	 * 新建按钮触发事件
	 */
	public void create() {
		created = new MainProtectQuit();
		created.setChannelBreakTime(new Date());
		created.setExecutionTime(new Date());
	}

	public void saveNew() {
		if (created != null) {
			created = ma.creat(created);
			outList.add(0, created);
		}
		addSuccessMessage("保存成功");
	}

	public void onCellEdit(CellEditEvent event) {
		for (MainProtectQuit k : outList) {
			if (k.getId().equals(event.getRowKey())) {
				selected = k;
				break;
			}
		}
		if (selected != null) {
			selected = ma.update(selected);
		}
	}

	public void update() {
		if (selected != null) {
			selected = ma.update(selected);
			addSuccessMessage("保存成功");
		}
	}

	public void delete() {
		if (selected != null) {
			ma.delete(selected.getId());
			outList.remove(selected);
		} else {
			addErrorMessage("请选择一条数据进行删除");
		}
		selected = null;
	}

	public List<String> completeSubControlArea(String query) {
		HashSet<String> set = new HashSet<String>();
		for (String s : subareas) {
			if (s != null && s.contains(query)) {
				set.add(s);
			}
		}
		List<String> res = new ArrayList<>();
		res.addAll(set);
		return res;
	}

	public void onRowSelect(SelectEvent event) {
		selected = (MainProtectQuit) event.getObject();
	}

	public List<MainProtectQuit> getOutList() {
		return outList;
	}

	public void setOutList(List<MainProtectQuit> outList) {
		this.outList = outList;
	}

	public MainProtectQuit getCreated() {
		return created;
	}

	public void setCreated(MainProtectQuit created) {
		this.created = created;
	}

	public MainProtectQuit getSelected() {
		return selected;
	}

	public void setSelected(MainProtectQuit selected) {
		this.selected = selected;
	}

	public List<String> getSubareas() {
		return subareas;
	}

	public void setSubareas(List<String> subareas) {
		this.subareas = subareas;
	}

}
